package com.zsk.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsk.entity.StudentEntity;
import com.zsk.mapper.StudentEntityMapper;
import com.zsk.service.IStudentEntityService;

/**
 * <p>
 * 学生表 前端控制器
 * </p>
 *
 * @author keke
 * @since 2020-07-24
 */
@RestController
@RequestMapping("/student-entity")
public class StudentEntityController {

    @Autowired
    IStudentEntityService studentService;

    @Autowired
    StudentEntityMapper studentMapper;

    @GetMapping("/students")
    public List<StudentEntity> list() {
        return studentService.list();
    }

    @PostMapping("/student")
    public boolean add(@RequestBody StudentEntity student) {
        return studentService.save(student);
    }

    @DeleteMapping("/student/{id}")
    public boolean delete(@PathVariable String id) {
        return studentService.removeById(id);
    }

    @PutMapping("/student/{id}")
    public boolean update(@PathVariable String id, int age, String className) {
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id).orderByDesc("create_time");
        StudentEntity student = new StudentEntity();
        student.setUClass(className);
        student.setUAge(age);
        return studentService.update(student, wrapper);
    }

    @GetMapping("/student/{pageIndex}/{pageSize}")
    public IPage<StudentEntity> getStudent(@PathVariable int pageIndex, @PathVariable int pageSize,
        @RequestParam(required = false) String className) {
        IPage<StudentEntity> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setSize(pageSize);
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(className != null, "u_class", className).orderByDesc("create_time");
        return studentService.page(page, wrapper);
    }

    @PostMapping("/mystudent")
    public boolean myAdd(@RequestBody StudentEntity student) {
        return studentMapper.myInsert(student) > 0;
    }

    @PostMapping("/mystudent/delete/{id}")
    public boolean myDelete(@PathVariable String id) {
        return studentMapper.myDelete(id) > 0;
    }

    @GetMapping("/mystudent")
    public List<StudentEntity> myAdd(@RequestParam String userName) {
        return studentMapper.mySelectByUserName(userName);
    }

    @PutMapping("/mystudent/{id}")
    public boolean myUpdate(@PathVariable String id, int age, String className) {
        return studentMapper.myUpdate(id, className, age) > 0;
    }
}
